(function(window, angular, undefined){
	var Module = angular.module('water.apps.common.stationInfo.sitePollutantsManager', ['water.entity.sitePollutants','water.entity.safeUser']);
	
	Module.controller('commonSitePollutantsManagerController',function($rootScope,$scope,$sitePollutantsService,$safeUserService){

		/**
		 * 当前选定的站点对象
		 */
		$scope.stationModel = {};
		/**
		 * 编辑窗口对象
		 */
		$scope.editDialog;
		/**
		 * 报警周期设置窗口对象
		 */
		$scope.thresholdAlarmCycleSetDialog;
		
		/**
		 * 阈值角色用户绑定窗口
		 */
		$scope.thresholdRoleUserSetDialog;
		/**
		 * 报警周期阈值角色用户绑定窗口
		 */
		$scope.alarmCycleRoleUserSetDialog;
		/**
		 * 编辑对象
		 */
		$scope.pollEdit = {};
		/**
		 * 报警周期编辑对象
		 */
		$scope.thresholdEdit = {};
		/**
		 * 阈值指派编辑对象
		 */
		$scope.pollThresholdEdit = {};
		
		/**
		 * 报警周期对象
		 */
		$scope.alarmCycleObj = {};
		
		/**
		 * 选中指派的角色
		 */
		$scope.roles = [];
		
		$scope.users = [];
		$scope.thresholdTypeData = [{"k":"0","v":"实测"},{"k":"1","v":"折算"}];
		/**
		 * 监听站点列表点击事件
		 */
		$scope.dataPramChangedBroadCast =$scope.$on('dataPramChanged', function(event, data) {
			$scope.stationModel = data;
			$scope._getSitePollutantsList(data.STATION_ID);
	    });
		/**
		 * 编辑打开
		 */
		$scope.onEditPollClick = function(data){
			$scope._getSitePollModel(data.GUID);
			$scope.editDialog.open();
		}
		/**
		 * 提交编辑
		 */
		$scope.onSubmitEditPollClick = function(data){
			$sitePollutantsService.updateModelWithOutChild(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				 $scope._getSitePollutantsList($scope.stationModel.STATION_ID);
				$scope.editDialog.close();
			});
		}
		/**
		 * 删除阈值
		 */
		$scope.onDeleteAlarm = function(guid){
			$sitePollutantsService.deleteThreshold(guid).then(function(){
				$.messager.alert("删除成功","删除成功",'info');
				 $scope._getSitePollutantsList($scope.stationModel.STATION_ID);
			});
		}
		/**
		 * 新增阈值
		 */
		$scope.onSubmitCreateThresholdClick = function(sitePollId,mn,threshold){
			$sitePollutantsService.createThreshold(sitePollId,mn,threshold).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope._getSitePollModel(sitePollId);
				 $scope._getSitePollutantsList($scope.stationModel.STATION_ID);
			});
		}
		/**
		 * 编辑阈值
		 */
		$scope.onSubmitEditThresholdClick = function(threshold){
			$sitePollutantsService.updateThreshold(threshold).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope._getSitePollModel(threshold.SITE_POLLUTANTS_ID);
			});
		}
		/**
		 * 指派按钮点击事件
		 */
		$scope.onAssignClick = function(threshold){
			$scope.roles = ['20160912160629fe80f39a34644ee8873837a6494c6d05'];
			$sitePollutantsService.getThresholdModel(threshold.GUID).then(function(data){
				$scope.pollThresholdEdit.BC_POLL_ROLE_LIST = data.BC_POLL_ROLE_LIST;
				$scope.pollThresholdEdit = data;
				$scope.users = $scope.pollThresholdEdit.BC_POLL_ROLE_LIST;
			});
			$scope.thresholdRoleUserSetDialog.open();
		}
		/**
		 * 保存指派信息
		 */
		$scope.onSubmitEditThresholdWithChildClick = function(data){
			$sitePollutantsService.updateThresholdWithChild(data).then(function(){
				 $scope._getSitePollutantsList($scope.stationModel.STATION_ID);
				$.messager.alert("保存成功","保存成功",'info');
				$scope.thresholdRoleUserSetDialog.close();
			});
		}
		/**
		 * 设置标准阈值
		 */
		$scope.onSetStandardThresholdClick = function(id){
			$sitePollutantsService.setStandardThreshold(id).then(function(){
				$.messager.alert("设置成功","设置成功",'info');
				 $scope._getSitePollutantsList($scope.stationModel.STATION_ID);
			});
		}
		/**
		 * 删除用户
		 */
		$scope.onDeleteUserClick = function(userList,user){
			var temUser = angular.copy($scope.users);
			temUser.remove(user,"USER_ID");
			$scope.users = temUser;
			userList.remove(user,'USER_ID');
		}
		/**
		 * 增加阈值关联用户
		 */
		$scope.onAddThresholdUserClick = function(user,userItems,curClickItem){
			_addUser(user,userItems,curClickItem,$scope.pollThresholdEdit.BC_POLL_ROLE_LIST);
		}
		/**
		 * 根据角色获取用户，用户组件获取数据方法
		 */
		$scope.getUserByRoleId = function(roleId){
			return $safeUserService.getListByRoleId(roleId);
		}
		/**
		 * 获取站点污染物model
		 */
		 $scope._getSitePollModel=function(sitePollId){
			$sitePollutantsService.getModel(sitePollId).then(function(d){
				if(d){
					d.BC_POLL_THRESHOLD_LIST.push({isNew:true,POLLUTE_CODE:d.NAME});
					$scope.pollEdit = d;
				}				
			});
		}
		
		/**
		 * 获取站点列表
		 */
		 $scope._getSitePollutantsList=function (stationId){
			$sitePollutantsService.getList({STATION_ID:stationId}).then(function(d){
				$scope.sitePolls = d;
			});
		}
		/**
		 * 增加用户公共方法
		 */
		function _addUser(user,userItems,curClickItem,targetUserList){
			if(!curClickItem){
				return;
			}
			curClickItem.USER_ID = curClickItem.k;
			if(curClickItem.selected){
				if(targetUserList.indexOf(curClickItem,"USER_ID") > -1){
					return;
				}
				else{
					targetUserList.push({
						C_USEREALNAME:curClickItem.v,
						USER_ID:curClickItem.k
					});
				}
			}
			else{
				targetUserList.remove(curClickItem,"USER_ID");
			}
		}
/***************************报警周期***********************************/		
		/**
		 * 打开报警周期设置
		 */
		$scope.onEditThresholdAlarmCycleClick = function(data,pollName){
			$scope.pollName = pollName;
			_getThresholdModel(data);
			$scope.ThresholdAlarmCycleSetDialog.open();
		}
		
		/**
		 * 获取阈值model
		 */
		function _getThresholdModel(guid){
			$sitePollutantsService.getThresholdModel(guid).then(function(data){
				if(data){
					data.POLL_ALARM_THRESHOLD_CYCLE_SET_LIST.push({isNew:true,POLLUTE_CODE:data.NAME});
					$scope.thresholdEdit = data;
				}				
			});
		}
		/**
		 * 新增报警周期
		 */
		$scope.onSubmitCreateAlarmCycleClick = function(thresholdGuid,cycleData){
			$sitePollutantsService.createThresholdAlarmCycle(thresholdGuid,cycleData).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				_getThresholdModel(thresholdGuid);
			});
		}
		
		/**
		 * 编辑报警周期
		 */
		$scope.onSubmitEditAlarmCycleClick = function(cycleData){
			$sitePollutantsService.updateThresholdAlarmCycle(cycleData).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				_getThresholdModel(thresholdGuid);
			});
		}	
		
		/**
		 * 周期设置指派按钮点击事件
		 */
		$scope.onAssignAlarmCycleClick = function(cycleData){
			$scope.roles = ['20160912160629fe80f39a34644ee8873837a6494c6d05'];
			$sitePollutantsService.getThresholdAlarmCycleModel(cycleData.GUID).then(function(data){
				$scope.alarmCycleObj.BC_POLL_ROLE_LIST = data.BC_POLL_ROLE_LIST;
				$scope.alarmCycleObj = data;
				$scope.users = [];
				$scope.users = $scope.alarmCycleObj.BC_POLL_ROLE_LIST;
			});
			$scope.alarmCycleRoleUserSetDialog.open();
		}
		/**
		 * 增加阈值关联用户
		 */
		$scope.onAddCycleUserClick = function(user,userItems,curClickItem){
			_addUser(user,userItems,curClickItem,$scope.alarmCycleObj.BC_POLL_ROLE_LIST);
		}
		/**
		 * 报警周期用户指派信息保存
		 */
		$scope.onSubmitEditAlarmCycleUserClick = function(data) {
			$sitePollutantsService.updateThresholdAlarmCycleWithChild(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope.alarmCycleRoleUserSetDialog.close();
			});
		}
	});
}(window, angular));	